using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace Alogient.Cameleon.Forms.Sdk.Items
{
    public class SubmissionValue
    {
        public int ID { get; private set; }
        public int SubmissionID { get; set; }
        public int FormElementID { get; set; }
        public string Value { get; set; }
        public Submission Submission = null;

        public FormElement FormElement(string cultureCode)
        {
            if (FormElementID > 0)
            {
                return new FormElement(FormElementID, cultureCode);
            }
            return null;
        }

        List<SubmissionValueItem> _items = new List<SubmissionValueItem>();
        public List<SubmissionValueItem> Items
        {
            get
            {
                return _items;
            }
        }

        public SubmissionValue()
        {
            Value = string.Empty;
            FormElementID = 0;
            SubmissionID = 0;
            ID = 0;
        }
        
        internal bool Save()
        {
            if (ID == 0) //New Submission
            {
                FormElement element = new FormElement(FormElementID, Alogient.Cameleon.Sdk.NavigationController.GetCultureCode());
                if (element.InputType == FormInputType.Upload)
                {
                    Controls.FormUpload upload = this.Submission.CMForm.FindControl(string.Format(Controls.CMFormElement.elementIDFormat, FormElementID.ToString())) as Controls.FormUpload;
                    string fileName = upload.UploadFile();
                    this.Value = fileName;
                }

                throw new NotImplementedException("Not implemented"); // TODO

                //foreach (SubmissionValueItem item in _items)
                //{
                //    item.SubmissionValueID = ID;
                //    item.Save();
                //}

                //return true;
            }

            return false;
        }
    }
}
